package com.yaoran.userauth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yaoran.userauth.dao.RoleDao;
import com.yaoran.userauth.entity.Role;
import com.yaoran.userauth.service.RoleService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.R;
import com.yaoran.userauth.utils.ThreadLocalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    private final static Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleDao roleDao;

    @Override
    public void addRole(Role role, Date createTime){
        if (createTime != null){
            role.setCreateTime(createTime);
        }else {
            role.setCreateTime(new Date());
        }
        role.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        role.setCreateBy(name);
        roleDao.addRole(role);
    }

    @Override
    public PageUtils<Role> listRole(Query query,String keyWord){
        Page<Role> page = PageHelper.startPage(query.getPage(),query.getLimit());
        List<Role> list = roleDao.listRole(keyWord);
        int total = (int) page.getTotal();
        return new PageUtils(list,total,query.getLimit(),query.getPage());
    }

    @Override
    public void updateRole(Role role){
        role.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        role.setUpdateBy(name);
        roleDao.updateRole(role);
    }

    @Override
    public void deleteRole(Long id){
        roleDao.deleteRole(id);
    }

    @Override
    public void deleteManyRole(Long[] ids){
        roleDao.deleteManyRoles(ids);
    }

    @Override
    public Role getRole(Long id){
        return roleDao.getRole(id);
    }

    @Override
    public List<Role> listAllRole(){
        return roleDao.listRole();
    }
}
